import React from 'react';
import FieldContext from "./FieldContext";
import useForm  from './useForm';
function Form({initialValues,onFinish,onFinishFailed,children}) {
    const [formInstance] = useForm();
    formInstance.setCallbacks({
        onFinish,
        onFinishFailed
    })
    const mountRef = React.useRef(null);
    formInstance.setInitialValues(initialValues, !mountRef.current);
    if (!mountRef.current) {
        mountRef.current = true;
    }
    return (
        <form
          onSubmit={event => {
            event.preventDefault();
            event.stopPropagation();
            formInstance.submit();
          }}>
          <FieldContext.Provider value={formInstance}>
            {children}
          </FieldContext.Provider>
        </form>
      );
}

export default Form;
